{% extends "templates/base.html" %}
{% from "www/macros/livecode.html" import LiveCodeEditorJS, LiveCodeEditor with context %}
{% block title %} {{ lesson.title }} - {{ course.title }} {% endblock %}

{% block head_include %}
<link rel="stylesheet" href="/assets/frappe/css/hljs-night-owl.css">

{% for ext in page_extensions %}
{{ ext.render_header() }}
{% endfor %}

{% endblock %}


{% block content %}
<div class="common-page-style">
  <div class="container course-details-page">
    {{ widgets.BreadCrumb(course=course, lesson=lesson) }}
    <div class="course-content-parent">
      <div class="course-details-outline">
        {{ widgets.CourseOutline(course=course, membership=membership) }}
      </div>
      <div class="lesson-pagination-parent">
        {{ LessonContent(lesson) }}
        {% if membership %}
        {{ pagination(prev_url, next_url) }}
        {% endif %}
      </div>
    </div>
    {{ Discussions() }}
  </div>
</div>
{% endblock %}

{% macro LessonContent(lesson) %}
{% set is_instructor = frappe.session.user == course.instructor %}
<div class="lesson-content">
  <div class="course-home-headings title
  {% if membership %} is-member {% endif %}
  {% if membership or is_instructor %} eligible-for-submission {% endif %}" data-lesson="{{ lesson.name }}"
    data-course="{{ course.name }}">
    {{ lesson.title }}
    <span class="lesson-progress {{hide if course.get_progress(lesson.name) != 'Complete' else ''}}">COMPLETED</span>
  </div>


  {% if membership or lesson.include_in_preview or is_instructor %}
  <div class="common-card-style lesson-content-card markdown-source">
    {% if is_instructor and not lesson.include_in_preview %}
    <small class="alert alert-secondary alert-dismissible">
      This lesson is not available for preview. As you are the Instructor of the course only you can see it.
      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    </small>
    {% endif %}
    {{ lesson.render_html() }}</div>
  {% else %}
  <div class="common-card-style lesson-content-card">
    <div class="w-25 text-center" style="margin: 0 auto;">
      <small>This lesson is not available for preview. Please join the course to access it.</small>
      <a class="button is-primary ml-auto mr-auto mt-3" href="/courses/{{ course.name }}"> Start Learning </a>
    </div>
  </div>
  {% endif %}

</div>
{% endmacro %}

{% macro pagination(prev_url, next_url) %}
<div class="lesson-pagination">

  <div>
    {% if prev_url %}
    <a class="button is-secondary dark-links prev" href="{{ prev_url }}">
      <img class="mr-2" src="/assets/school/icons/left-arrow.svg">
      Prev
    </a>
    {% endif %}
  </div>


  {% if not course.is_mentor(frappe.session.user) and membership %}
  {% set progress = course.get_progress(lesson.name) %}
  <div class="custom-checkbox {% if progress == 'Complete' %} hide {% endif %}">
    <label class="quiz-label">
      <input class="option mark-progress" type="checkbox" checked>
      <img class="empty-checkbox" />
      <span class="small">{{ _("Mark as complete on moving to the next lesson") }}</span>
    </label>
  </div>

  <div class="button is-secondary mark-progress {{ progress }} {% if progress == 'Incomplete' or progress == None %} hide {% endif %}"
    data-progress="Incomplete">
    Mark as Incomplete
  </div>

  {% endif %}

  <div>
    <a class="button is-primary next {% if membership.progress|int == 100 and not next_url %} hide {% endif %}"
      {% if next_url %} data-href="{{ next_url }}" {% endif %} href="">
      {% if next_url %} Next {% else %} Mark as Complete {% endif %}
      <img class="ml-2" src="/assets/school/icons/side-arrow-white.svg">
    </a>
    {% if course.enable_certification %}
    <div class="button is-primary {% if membership.progress|int != 100 or next_url %} hide {% endif %}" id="certification">
      Get Certificate
    </div>
    {% endif %}
  </div>

</div>
{% endmacro %}

{% macro Discussions() %}
{% set is_instructor = frappe.session.user == course.instructor %}
{% set condition = is_instructor if is_instructor else membership %}
{% set doctype, docname = "Course Lesson", lesson.name %}
{% set title = "Questions" %}
{% set cta_title = "Ask a Question" %}
{% set button_name = "Start Learning" %}
{% set redirect_to = "/courses/" + course.name %}
{% set empty_state_title = "Have a doubt?" %}
{% set empty_state_subtitle = "Post it here, our mentors will help you out." %}

{% include "frappe/templates/discussions/discussions_section.html" %}
{% endmacro %}

{%- block script %}
{{ super() }}

<script type="text/javascript">
  var page_context = {{ page_context | tojson }};
</script>

{% for ext in page_extensions %}
{{ ext.render_footer() }}
{% endfor %}
{%- endblock %}
